package ai.people.core.web.exception.handler;

import ai.people.netmon.framework.exception.enums.ParamExceptionEnum;
import ai.people.netmon.framework.exception.type.*;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常处理程序
 *
 * @author yuanqinglong
 * @date 2022/4/15 18:10
 */
@Order(20)
@RestControllerAdvice
public class CustomizeExceptionHandler {

    Logger logger = LoggerFactory.getLogger(CustomizeExceptionHandler.class);

    /**
     * 方法无效异常
     *
     * @param methodNotValidException 业务异常
     * @return {@link Result}<{@link T}>
     */
    @ExceptionHandler(MethodNotValidException.class)
    public <T> Result<T> methodNotValidException(MethodNotValidException methodNotValidException) {
        //记录日志
        logger.error("businessException catch exception", methodNotValidException);
        return ResultUtil.error(ParamExceptionEnum.INSUFFICIENT_PARAMETERS.getCode(), methodNotValidException.getMessage());
    }

    /**
     * 业务异常
     *
     * @param businessException 业务异常
     * @return {@link Result}<{@link T}>
     */
    @ExceptionHandler(BusinessException.class)
    public <T> Result<T> businessException(BusinessException businessException) {
        //记录日志
        logger.error("businessException catch exception", businessException);
        return ResultUtil.error(businessException);
    }


    /**
     * 自定义运行时异常
     *
     * @param customizeRuntimeException 自定义运行时异常
     * @return {@link Result}<{@link T}>
     */
    @ExceptionHandler(CustomizeRuntimeException.class)
    public <T> Result<T> customizeRuntimeException(CustomizeRuntimeException customizeRuntimeException) {
        //记录日志
        logger.error("catch exception", customizeRuntimeException);
        return ResultUtil.error(customizeRuntimeException);
    }


    /**
     * 身份验证异常
     *
     * @param authException 身份验证异常
     * @return {@link Result}<{@link T}>
     */
    @ExceptionHandler(AuthException.class)
    public <T> Result<T> authException(AuthException authException) {
        authException.printStackTrace();
        //记录日志
        logger.error("catch exception ", authException);
        return ResultUtil.error(authException);
    }


    /**
     * 目标档案异常
     *
     * @param archivesException 档案例外
     * @return {@link Result}<{@link T}>
     */
    @ExceptionHandler(ArchivesException.class)
    public <T> Result<T> archivesException(ArchivesException archivesException) {
        archivesException.printStackTrace();
        //记录日志
        logger.error("catch exception", archivesException);
        return ResultUtil.error(archivesException);
    }

}
